package testCase;

import com.robotium.solo.Solo;
import android.os.Build;
import android.support.test.uiautomator.UiDevice;
import android.test.ActivityInstrumentationTestCase2;
import android.util.Log;
import elements.TestCase_entrance;
import utils.GetJsonValue;
import utils.MyTestResult;
import utils.UserInfo_Properties;
import utils.Util;
import utils.MyException.RunTestException;
import utils.MyException.SetUpException;
import utils.MyException.TearDownException;

@SuppressWarnings("rawtypes")
public class HeadTestCase extends ActivityInstrumentationTestCase2 {

	public Util util;
	public MyTestResult result;
	public TestCase_entrance entrance;
	public GetJsonValue getJson;
	public Solo solo;
	
	public static final int NUMBEROFRETRIES = 1;// 测试用例失败重试次数
	public String cl_Name = this.getClass().getSimpleName() + "/";
	public static final String TAG = UserInfo_Properties.TAG;
	
	public static final String LINE_SEPARATOR = System.getProperty("line.separator");// 换行符
	public static final String DEVICE_MODEL = Build.MODEL; // 设备型号
	public static final int VERSION_SDK = Build.VERSION.SDK_INT; // 设备SDK版本
	public static final String VERSION_RELEASE = Build.VERSION.RELEASE; // 设备的系统版本
	private final static String RE_LAUNCHER_CMD = 
			"";
	private final static String RE_LAUNCHER_CMD$ = 
			"";
	private final static String LAUNCHER_ACTIVITY_FULL_NAME = 
			"";

	private static Class<?> launcherActivity;
	static {
		try {
			launcherActivity = Class.forName(LAUNCHER_ACTIVITY_FULL_NAME);
		} catch (ClassNotFoundException e) {
			Log.e(TAG, "LAUNCHER_ACTIVITY_FULL_NAME is fail!!!!");
			e.printStackTrace();
		}
	}

	@SuppressWarnings("unchecked")
	public HeadTestCase() {
		super(launcherActivity);
	}

	@Override
	protected void setUp() throws Exception {
		try {
			super.setUp();
			util = new Util();
			getJson = new GetJsonValue();
			result = new MyTestResult();
			// 唤醒屏幕
			UiDevice uiDevice=UiDevice.getInstance(getInstrumentation());
			uiDevice.wakeUp();
			solo = new Solo(getInstrumentation(), getActivity());
			// 如果屏幕被锁，则解锁屏幕
			solo.unlockScreen();
			entrance = new TestCase_entrance(solo);
		} catch (Throwable t) {
			throw new SetUpException(t);
		}
	}

	@Override
	public void runBare() throws Throwable {
		try {
			super.runBare();
		} catch (SetUpException se) {
			solo.takeScreenshot(this.getClass().getSimpleName());
			this.tearDown();
			throw se;
		}
	}

	@Override
	protected void runTest() throws Throwable {
		int times = NUMBEROFRETRIES;
		boolean screanshot = true;

		while (times > 0) {
			try {
				super.runTest();
				break;
			} catch (Exception e) {
				if (times > 1) {
					times--;
					tearDown();
					Runtime run = Runtime.getRuntime();
					try {
						if (Build.VERSION.SDK_INT >= 17) {
							run.exec(RE_LAUNCHER_CMD);
						} else {
							run.exec(RE_LAUNCHER_CMD$);
						}
					} catch (Exception e1) {
						Log.e(TAG, "启动失败");
						throw new RunTestException(e1);
					}
					setUp();
					continue;
				} else {
					if (screanshot) {
						result.printTestResultToExcel(cl_Name + this.getName(), "测试过程操作失败或出现异常", "N/A", "error");
						solo.takeScreenshot(this.getClass().getSimpleName());
						Log.e(TAG, "测试执行过程中出现异常");
						throw new RunTestException(e);
					}
				}
			}
		}
	}

	@Override
	protected void tearDown() throws Exception {
		try {
			super.tearDown();
			entrance = null;
			result = null;
			getJson = null;
			util = null;
			solo.finishOpenedActivities();
		} catch (Throwable t) {
			solo.takeScreenshot(this.getClass().getSimpleName());
			throw new TearDownException(t);
		}
	}
}